package com.lilili.ssh.login.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.lilili.ssh.login.dao.LoginDao;
import com.lilili.ssh.login.model.SysInfo;
import com.lilili.ssh.user.model.User;

public class LoginDaoImpl extends HibernateDaoSupport implements LoginDao{

	public User chkLogin(User user){
		String hql="from User user where user.loginName=:loginName and user.password=:password";
		Session s=this.getSession();
		Query query=s.createQuery(hql);
		query.setString("loginName", user.getLoginName());
		query.setString("password", user.getPassword());
		List<User> objList=query.list();
	    if(objList.size()>0){
	    	return objList.get(0);
	    }
		return user;
	}
	
	public SysInfo getSysInfo(){
		SysInfo sysInfo=new SysInfo();
		User user;
		List userList;
		List bookList;
		String availableUsers="";
		String notAvailableUsers="";
		String booksInStore="";
		String booksByUser="";
		String abandonBooks="";
		try {
			availableUsers = getHibernateTemplate().find("select count(*) as rowCount from User user where user.statu=1").get(0).toString();
			notAvailableUsers = getHibernateTemplate().find("select count(*) as rowCount from User user where user.statu=-1").get(0).toString();
			booksInStore = getHibernateTemplate().find("select count(*) as rowCount from Book book where book.statu=1").get(0).toString();
			booksByUser = getHibernateTemplate().find("select count(*) as rowCount from Book book where book.statu=-1").get(0).toString();
			abandonBooks = getHibernateTemplate().find("select count(*) as rowCount from Book book where book.statu=0").get(0).toString();
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		sysInfo.setAvailableUsers(availableUsers);
		sysInfo.setNotAvailableUsers(notAvailableUsers);
		sysInfo.setBooksInStore(booksInStore);
		sysInfo.setBooksByUser(booksByUser);
		sysInfo.setAbandonBooks(abandonBooks);
		return sysInfo;
	}
}
